﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using RTGS.Library;
using RTGS.Library.Entities.Rtgs;
using RTGS.Library.Enum;
using RTGS.Library.Interfaces;

namespace RTGS.Library.DataAccess.Rtgs
{
    public partial class stpRecordDetailIRepo
    {
        public bool UpdateStatus(stpRecordDetailI domain)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sql = new StringBuilder();

                    sql.AppendLine("UPDATE STP_RECORD_DETAIL_I SET RECORD_DETAIL_IST_Status = @recordDetailIstStatus");
                    sql.AppendLine(" WHERE RECORD_DETAIL_ITR_RelatedTRN = @recordDetailItrRelatedtrn");

                    cmd.CommandText = sql.ToString();

                    cmd.Parameters.Add("@recordDetailIstStatus", SqlDbType.Char).Value = (object)domain.recordDetailIstStatus ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailItrRelatedtrn", SqlDbType.VarChar).Value = domain.recordDetailItrRelatedtrn;


                    try
                    {
                        conn.Open();
                        result = cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return (result > 0);
        }

        public bool UpdateStatusCancel(stpRecordDetailI domain)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sql = new StringBuilder();

                    sql.AppendLine("UPDATE STP_RECORD_DETAIL_I SET RECORD_DETAIL_IST_Status = @recordDetailIstStatus,RECORD_DETAIL_IST_RsnCde=@recordDetailIstRsncde");
                    sql.AppendLine(" WHERE RECORD_DETAIL_ITR_RelatedTRN = @recordDetailItrRelatedtrn");

                    cmd.CommandText = sql.ToString();

                    cmd.Parameters.Add("@recordDetailIstStatus", SqlDbType.Char).Value = (object)domain.recordDetailIstStatus ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailItrRelatedtrn", SqlDbType.VarChar).Value = domain.recordDetailItrRelatedtrn;
                    cmd.Parameters.Add("@recordDetailIstRsncde", SqlDbType.VarChar).Value = (object)domain.recordDetailIstRsncde ?? DBNull.Value;


                    try
                    {
                        conn.Open();
                        result = cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return (result > 0);
        }

        public bool UpdateIncomingRepair(stpRecordDetailI domain, string reasoncode)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sql = new StringBuilder();

                    sql.AppendLine("UPDATE STP_RECORD_DETAIL_I SET RECORD_DETAIL_IST_Status = @recordDetailIstStatus");
                   
                        if (reasoncode == "011")
                        {
                            sql.AppendLine(",RECORD_DETAIL_IST_UltimateBenefName = @recordDetailIstUltimatebenefname");
                            sql.AppendLine(",RECORD_DETAIL_IST_UltimateBenefAccount = @recordDetailIstUltimatebenefaccount");

                            cmd.Parameters.Add("@recordDetailIstUltimatebenefname", SqlDbType.VarChar).Value = (object)domain.recordDetailIstUltimatebenefname ?? DBNull.Value;
                            cmd.Parameters.Add("@recordDetailIstUltimatebenefaccount", SqlDbType.VarChar).Value = (object)domain.recordDetailIstUltimatebenefaccount ?? DBNull.Value;

                        }

                        if (reasoncode == "012")
                        {
                            sql.AppendLine(",RECORD_DETAIL_IST_OriginatingName = @recordDetailIstOriginatingname");
                            sql.AppendLine(",RECORD_DETAIL_IST_OriginatingNumber = @recordDetailIstOriginatingnumber");

                            cmd.Parameters.Add("@recordDetailIstOriginatingname", SqlDbType.VarChar).Value = (object)domain.recordDetailIstOriginatingname ?? DBNull.Value;
                            cmd.Parameters.Add("@recordDetailIstOriginatingnumber", SqlDbType.VarChar).Value = (object)domain.recordDetailIstOriginatingnumber ?? DBNull.Value;
                        }



                    sql.AppendLine(",RECORD_DETAIL_IST_PaymentDetails = @recordDetailIstPaymentdetails");
                    sql.AppendLine(" WHERE RECORD_DETAIL_ITR_RelatedTRN = @recordDetailItrRelatedtrn");

                    cmd.CommandText = sql.ToString();

                    cmd.Parameters.Add("@recordDetailIstStatus", SqlDbType.Char).Value = (object)domain.recordDetailIstStatus ?? DBNull.Value;
                    cmd.Parameters.Add("@recordDetailItrRelatedtrn", SqlDbType.VarChar).Value = domain.recordDetailItrRelatedtrn;
                    cmd.Parameters.Add("@recordDetailIstPaymentdetails", SqlDbType.VarChar).Value = (object)domain.recordDetailIstPaymentdetails ?? DBNull.Value;

                    try
                    {
                        conn.Open();
                        result = cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        errorLog = ex.Message;
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return (result > 0);
        }
    }
}
